import path from 'path';
import { fileURLToPath } from 'url';
import { screen, MatchRequest, FileType, imageResource } from '@nut-tree/nut-js';
import ImageReader from '@nut-tree/template-matcher/dist/lib/image-reader.class.js';
import TemplateMatchingFinder from '@nut-tree/template-matcher/dist/lib/template-matching-finder.class.js';
import '@nut-tree/template-matcher';

const getDirname = (url) => {
    return path.dirname(fileURLToPath(new URL(url)));
}
const currentDir = getDirname(import.meta.url);

screen.config.resourceDirectory = './src/assets/';
screen.config.autoHighlight = true;
screen.config.highlightDurationMs = 6000;

const region = await screen.find(await imageResource('icon-screenshot.png'), { confidence: 0.98  });
console.log(region);
// await screen.capture('screenshot.png', FileType.PNG, path.resolve(currentDir, './assets'));

// const imageReader = new ImageReader.default();
// const templateMatchingFinder = new TemplateMatchingFinder.default();
// const haystackPath = path.resolve(currentDir, './assets/screenshot.png');
// const needlePath = path.resolve(currentDir, './assets/icon-screenshot.png');
// const haystack = await imageReader.load(haystackPath);
// const needle = await imageReader.load(needlePath);
// const miniConfidence = 0.99;
// const matchRequest = new MatchRequest(haystack, needle, miniConfidence);

// const matchResult = await templateMatchingFinder.findMatch(matchRequest);
// console.log(matchResult);
// await screen.highlight(matchResult.location);